package edu.zut.cs.software.example.shop.web.spring.controller;

import java.util.HashMap;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.market.domain.Product;
import edu.zut.cs.software.example.shop.dao.ShopDao;
import edu.zut.cs.software.example.shop.domin.Shop;
import edu.zut.cs.software.example.shop.service.ShopManager; 
/**
 * 
 * @author 张应鹏
 *
 */
@RestController
public class ShopController{
	private static final String String = null;

	@Autowired
	ShopDao shopDao;
	
	@Autowired
	ShopManager shopManager;
	
	@RequestMapping("/shop")//访问路径
	public String getShop() {
		String flag="eror";
		List<Shop> sh=shopDao.getByShopnamebusinessname();//获取一个shop对象
		System.out.println("shop"+sh);//输出这个sh
		HashMap<String,Object>res=new HashMap<>();//新建一个HashMap对象
		if(sh!=null) {
			flag="ok";
		}
		res.put("flag",flag);//添加信息
		res.put("shop",sh);
		System.out.println("res:"+res);
		String res_json=JSON.toJSONString(res);//把对象转化为JSON状
		System.out.println(res_json);
		return res_json;
	}
	
	
	//添加操作
	@RequestMapping("/addshop")//访问路径
	public String addShop(@RequestBody Shop shop) {
		System.out.println("访问成功");
		Shop addShop = shopDao.save(shop);
		return null;
		
}
	
	
	//删除操作
	@Modifying
	@Transactional
	@RequestMapping("/deleteShopID")//访问路径
	
	public String deleteShopID(String shopID) {
			
		System.out.println("*****ID******:" + shopID);
    	int s=shopDao.deleteShopID(shopID); 
    	System.out.println("*****result******:" + s);
    	String str = s>0?"success":"error";
    	System.out.println("*****result******:" + str);
        return str;
    }
	
	
	
	
	
	
	
	 @RequestMapping("/shop2")
	 	public String shop2(QueryInfo queryInfo) {
	 		System.out.println("前端携带数据： "+queryInfo+" 请求/getByStudentId");
	 
	 		
	 		int numbers =shopDao.getShopByStudentIdCounts(queryInfo.getId());
	 		System.out.println("numbers:"+numbers);
	 		
	 		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//	 		System.out.println("pageStart:"+pageStart);
	 		
	 		List<Shop> shops=shopDao.findByStudentId(queryInfo.getId(), pageStart, queryInfo.getPageSize());
	 		System.out.println("查询结果列表："+shops);
	 		
	 		HashMap<String,Object> res=new HashMap<>();
	 		res.put("numbers", numbers);
	 		res.put("data", shops);
	 		
	 		String res_string=JSON.toJSONString(res);
	 		
//	 		System.out.println("HashMap res: "+res);
//	 		System.out.println("转换数据格式后 res_string： "+res_string);
	 		
	 		return res_string;
	 	}
	//修改操作
	@RequestMapping("/getUpdateShop")//访问路径
	public String getUpdateShop(int id) {
		return JSON.toJSONString(shopManager.getUpdateShop(id));
		
	}
	//修改查询操作
	@RequestMapping("/editShop")//访问路径
	public String editShop(@RequestBody Shop shop) {
		
		System.out.println("____"+shop);
    String flag="error";
		
		int res=shopManager.editShop(shop);
		System.out.println(res);
		if(res==1) flag="success";
		
		HashMap<String,Object> res1=new HashMap<String, Object>();
		res1.put("flag", flag);
		
		int id=Integer.parseInt(String.valueOf(shop.getId()));
		Shop newShopInfo=shopManager.getUpdateShop(id);
		
		res1.put("shop", newShopInfo);
		String r =  JSON.toJSONString(res1);

		return r;
	}
	
	//查询操作
	@RequestMapping("/getShop")
	public String getShopList(QueryInfo queryInfo) {
		System.out.println("前端携带数据： "+queryInfo+" 请求/getShop");
		
		int numbers = shopDao.getshopCounts();
		System.out.println("numbers:"+numbers);
		
		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
		System.out.println("pageStart:"+pageStart);
		
		List<Shop> Shop= shopDao.getByShop(pageStart, queryInfo.getPageSize());
		System.out.println("查询结果列表："+Shop);
		
		HashMap<String,Object> res=new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Shop);
		
		String res_string=JSON.toJSONString(res);
		
//		System.out.println("HashMap res: "+res);
//		System.out.println("转换数据格式后 res_string： "+res_string);
		
		return res_string;
	}
}
